//
//  ThemeViewController.m
//  IconMaster
//
//  Created by mac on 2017/7/25.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "ThemeViewController.h"
#import "ThemeCollectionViewCell.h"
#import "ThemeHeaderCollectionReusableView.h"
#import "EditeNotAppViewController.h"
#import "EditeAppViewController.h"
#import "CImageUtil.h"
#import "CIconView.h"
#import "MongooseDaemon.h"
#import "RMStepsController.h"
#import "PrivateCustomViewController.h"
#import "SixIconView.h"
#define CELL_ID @"themecell"
#define Header_ID @"themeHeader"
@interface ThemeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, SixIconViewDelegate>{

    NSString *imgString;
    UIImage *image_imageStr;
}
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) SixIconView *sixIconView;
@property (nonatomic, strong) NSDictionary *inputDict;

@property (nonatomic, strong) NSDictionary *themeDict;

@end

@implementation ThemeViewController

- (void)getThemeData{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [IconmasterHttp GreatNetWorkingRequestType:GreatRequestType_GET andurl:AlbumGet andparameters:@{@"id":self.themeID} andcomplete:^(id json, NSError *error) {
        if ([json[@"code"] integerValue] == 200) {
            _themeDict = json[@"data"][@"album"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.collectionView reloadData];
        self.title =  [NSLocalizedString(@"ZH_EN", "") isEqualToString:@"zh"]? _themeDict[@"chineseName"]:_themeDict[@"englishName"];
        }
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarType];
    [self getThemeData];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.title = DataWithZHOrEn(_themeDict[@"chineseName"], _themeDict[@"englishName"]); //[NSLocalizedString(@"ZH_EN", "") isEqualToString:@"zh"]? _themeDict[@"chineseName"]:_themeDict[@"englishName"];
    UIImageView *imageView_backGround = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"headbackground"]];
    imageView_backGround.frame = CGRectMake(0, 0, KWidth, 200 + 64);
    [self.view addSubview:imageView_backGround];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 1.设置列间距
    layout.minimumInteritemSpacing = 8;
    // 2.设置行间距
    layout.minimumLineSpacing = 5;
    // 3.设置每个item的大小
    layout.itemSize = CGSizeMake((KWidth - 5*8)/4, (KWidth - 5*8)/4);
    // 4.设置Item的估计大小,用于动态设置item的大小，结合自动布局（self-sizing-cell）
//    layout.estimatedItemSize = CGSizeMake(KWidth, 120);
//    // 5.设置布局方向
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    // 6.设置头视图尺寸大小
//    layout.headerReferenceSize = CGSizeMake(50, 50);
//    // 7.设置尾视图尺寸大小
//    layout.footerReferenceSize = CGSizeMake(50, 50);
//    // 8.设置分区(组)的EdgeInset（四边距）
//    layout.sectionInset = UIEdgeInsetsMake(10, 20, 30, 40);
//    // 9.10.设置分区的头视图和尾视图是否始终固定在屏幕上边和下边
//    layout.sectionFootersPinToVisibleBounds = YES;
//    layout.sectionHeadersPinToVisibleBounds = YES;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, KWidth, KHeight- 64)collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[ThemeCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[ThemeHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Header_ID];
    // Do any additional setup after loading the view.
}
//设置分区数（必须实现）
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//设置每个分区的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    NSArray *arr = _themeDict[@"iconList"];
    return arr.count;  
}

//设置返回每个item的属性必须实现）
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ThemeCollectionViewCell *cell = (ThemeCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];  
//    cell.backgroundColor = [UIColor whiteColor];
//    cell.layer.shadowColor = [UIColor blackColor].CGColor;
//    cell.layer.shadowOpacity = 0.8f;
//    cell.layer.shadowOffset = CGSizeMake(4,4);
    [cell cellWithData:_themeDict[@"iconList"][indexPath.row]];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    return CGSizeMake(KWidth, 210);
}
//这个也是最重要的方法 获取Header的 方法。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //从缓存中获取 Headercell topPicturePath
    ThemeHeaderCollectionReusableView *cell = (ThemeHeaderCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Header_ID forIndexPath:indexPath];
    if (_themeDict) {
        [cell creatHeaderView:@[[NSString stringWithFormat:@"%@%@",BaseURLImage,_themeDict[@"topPicturePath"]]]];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [self showAdmobInController:self];
    self.sixIconView = [[SixIconView alloc] initWithFrame:CGRectMake(0, 64, KWidth, KHeight - 64)];
    self.sixIconView.delegate = self;
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        imgString = [NSString stringWithFormat:@"%@%@", BaseURLImage,_themeDict[@"iconList"][indexPath.row]];
        image_imageStr = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgString]]];
    });
    
    [self.view addSubview:self.sixIconView];
}
- (void)sixIconViewButton:(NSString *)string{

//    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgString]]];
    if ([string isEqualToString: PriButton]) {
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        //将第二个控制器实例化，"SecondViewController"为我们设置的控制器的ID
        PrivateCustomViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"PrivateCustomView"];
        vc.themeImg = image_imageStr;
//        vc.isFromClickStr = string;
        [self.navigationController pushViewController:vc animated:YES];

//        PrivateCustomViewController *notApp = [[PrivateCustomViewController alloc] init];
////        notApp.customerImg = image;PrivateCustomView
//        [self.navigationController pushViewController:notApp animated:YES];
    } else if ([string isEqualToString:AppEdite]) {
            UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            
            //将第二个控制器实例化，"SecondViewController"为我们设置的控制器的ID
            EditeAppViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"editeApp"];
            vc.customerImg = image_imageStr;
            vc.isFromClickStr = string;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            //
             UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            
            //将第二个控制器实例化，"SecondViewController"为我们设置的控制器的ID
            EditeNotAppViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"editeNotApp"];
            vc.customerImg = image_imageStr;
            vc.isFromClickStr = string;
            [self.navigationController pushViewController:vc animated:YES];
        }
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
